//
//  ExerciseCell.m
//  CareRingApp
//
//  Created by Linktop on 2023/7/25.
//

#import "ExerciseCell.h"
#import "ConfigModel.h"

@implementation ExerciseCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self layOutBase];
        
    }
    return self;
}

-(void)setExerObj:(DBExercise *)exerObj
{
    _exerObj = exerObj;
    if (_exerObj) {
        
        self.exerTypeLbl.text = [_exerObj stringForExerType];
        self.timeLbl.text = [[self dateFormatter] stringFromDate:_exerObj.startDate];
        self.levelLbl.text = [_exerObj stringForLevel];
        [self.levelLbl sizeToFit];
        BOOL isend = _exerObj.isEnd;
        self.isendLbl.text = isend ? _L(L_EXER_ALREADY_END) : _L(L_EXER_WROKING);
        self.isendLbl.textColor = isend ? UIColor.redColor : UIColor.whiteColor;
        
        self.durationLbl.text = [_exerObj durationString:NO];
        
    }

    
}

- (NSDateFormatter *)dateFormatter
{
    static NSDateFormatter *execCelldateFormatter;
    if(!execCelldateFormatter){
        execCelldateFormatter = [NSDateFormatter new];
        execCelldateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
    }
    
    return execCelldateFormatter;
}

-(void)layOutBase
{
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    UIView *bgV = [UIView new];
    [bgV setBackgroundColor:ITEM_BG_COLOR];
    bgV.layer.masksToBounds = YES;
    bgV.layer.cornerRadius = ITEM_CORNOR_RADIUS;
    
    [self.contentView addSubview:bgV];

    [self.contentView addSubview:self.exerTypeLbl];
    [self.contentView addSubview:self.isendLbl];
    [self.contentView addSubview:self.levelLbl];
    [self.contentView addSubview:self.timeLbl];
    
    [bgV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.equalTo(bgV.superview);
        make.bottom.equalTo(bgV.superview.mas_bottom).inset(5);
    }];
    
    
    self.arrowBtn = [[QMUIButton alloc]init];
    [self.arrowBtn setImage:[UIImage imageNamed:@"bing_arrow"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.arrowBtn];
    
    [self.exerTypeLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(8);
        make.leading.equalTo(self.contentView.mas_leading).offset(10);
    }];
    
    [self.contentView addSubview:self.durationLbl];
    [self.durationLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.trailing.equalTo(self.levelLbl.mas_trailing);
        make.centerY.equalTo(self.timeLbl.mas_centerY);
        make.height.equalTo(@20);
    }];
    UIImageView *clock = [UIImageView new];
    clock.contentMode = UIViewContentModeScaleAspectFit;
    clock.image = [UIImage imageNamed:@"clock_icon_small"];
    [self.contentView addSubview:clock];
    [clock mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.durationLbl.mas_centerY);
        make.trailing.equalTo(self.durationLbl.mas_leading).inset(3);
        make.height.equalTo(@15);
        make.width.equalTo(clock.mas_height);
        make.leading.greaterThanOrEqualTo(self.timeLbl.mas_trailing).offset(15);
    }];
    
    [self.arrowBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView.mas_trailing).inset(10);
        make.centerY.equalTo(self.exerTypeLbl.mas_centerY);
    }];
    
    [self.timeLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.exerTypeLbl.mas_leading);
        make.top.equalTo(self.exerTypeLbl.mas_bottom).offset(3);
        
    }];
    
    [self.isendLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.arrowBtn.mas_trailing);
        make.centerY.equalTo(self.timeLbl.mas_centerY);
    }];
    
    [self.levelLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.exerTypeLbl.mas_leading);
        make.top.equalTo(self.timeLbl.mas_bottom).offset(3);
        make.height.equalTo(@30);
        make.width.greaterThanOrEqualTo(@100);
        
    }];
    
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(UILabel *)exerTypeLbl
{
    // 运动方式
    if (!_exerTypeLbl) {
        _exerTypeLbl = [UILabel new];
        _exerTypeLbl.font = [UIFont systemFontOfSize:18];
    }
    return _exerTypeLbl;
}

-(UILabel *)isendLbl
{
    // 是否结束
    if (!_isendLbl) {
        _isendLbl = [UILabel new];
        _isendLbl.textAlignment = NSTextAlignmentRight;
        _isendLbl.textColor = UIColor.redColor;
        _isendLbl.font = [UIFont systemFontOfSize:16];
    }
    return _isendLbl;
}
-(UILabel *)levelLbl
{
    // 强度
    if (!_levelLbl) {
        _levelLbl = [UILabel new];
        _levelLbl.layer.cornerRadius = 16;
        _levelLbl.layer.masksToBounds = YES;
        _levelLbl.backgroundColor = MAIN_BLUE;
        _levelLbl.numberOfLines = 2;
        _levelLbl.adjustsFontSizeToFitWidth = YES;
        _levelLbl.textAlignment = NSTextAlignmentCenter;
    }
    return _levelLbl;
}
-(UILabel *)timeLbl
{
    if (!_timeLbl) {
        _timeLbl = [UILabel new];
        _timeLbl.textColor = UIColor.lightGrayColor;
        _timeLbl.font = [UIFont systemFontOfSize:16];
    }
    return _timeLbl;
}

-(UILabel *)durationLbl
{
    if (!_durationLbl) {
        _durationLbl = [UILabel new];
        _durationLbl.textAlignment = NSTextAlignmentRight;
        _durationLbl.font = [UIFont systemFontOfSize:16];
        _durationLbl.adjustsFontSizeToFitWidth = YES;
        _durationLbl.textColor = UIColor.lightGrayColor;
    }
    return _durationLbl;
}

//-(UILabel *)dataSyncLbl
//{
//    if(!_dataSyncLbl) {
//        _dataSyncLbl = [UILabel new];
//        _dataSyncLbl.layer.cornerRadius = 16;
//        _dataSyncLbl.layer.masksToBounds = YES;
//        _dataSyncLbl.layer.borderColor = MAIN_BLUE.CGColor;
//        _dataSyncLbl.layer.borderWidth = 1.f;
//        _dataSyncLbl.backgroundColor = UIColor.clearColor;
//        _dataSyncLbl.numberOfLines = 2;
//        _dataSyncLbl.textColor = MAIN_BLUE;
//        _dataSyncLbl.adjustsFontSizeToFitWidth = YES;
//        _dataSyncLbl.textAlignment = NSTextAlignmentCenter;
//        _dataSyncLbl.adjustsFontSizeToFitWidth = YES;
//        _dataSyncLbl.text = _L(L_EXER_DATA_NOT_SYNC);
//
//    }
//    return _dataSyncLbl;
//}

@end
